package com.jswlw.controller.admin;

import com.jswlw.controller.BaseController;
import com.jswlw.service.base.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

import static com.jswlw.common.Constants.*;

/**
 * Created by Administrator on 2017/12/6.
 */
@RequestMapping("/")
@Controller
public class LoginController extends BaseController {

    @Autowired
    UserService adminUserService;

    /**
     * 登录页面
     *
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 登录
     *
     * @param loginName
     * @param password
     * @return
     */
    @RequestMapping(value = "/do_login", method = RequestMethod.POST)
    @ResponseBody
    public Map doLogin(HttpSession session, String loginName, String password) {
        Map map = adminUserService.login(loginName, password);
        if ((int) map.get(CODE) == 0) {
            session.setAttribute(USER_SESSION,map.get(DATA));
        }
        return map;
    }

    @RequestMapping("/login_out")
    public String loginOut(HttpSession session)
    {
        session.removeAttribute(USER_SESSION);
        return "login";
    }

}
